using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ICSharpCode.TextEditor.Formatting
{
    public class CSharpFormatStrategy : IFormattingStrategy
    {
        private void StringLineParse(List<string> list, string text)
        {
            if (text.StartsWith("//"))
            {
                list.Add(text);
            }
            else if (text.StartsWith("/*"))
            {
                list.Add(text);
            }
            else if (text.EndsWith("*/"))
            {
                list.Add(text);
            }
            else if (Regex.IsMatch(text, "^for(.+)$"))
            {
                list.Add(text);
            }
            else if (text.IndexOf('"') != -1)
            {
                list.Add(text);
            }
            else if (text.IndexOf(';') != -1)
            {
                string[] array = text.Split(';');
                string[] array2 = array;
                foreach (string text2 in array2)
                {
                    if (!string.IsNullOrEmpty(text2.Trim()))
                    {
                        list.Add(text2.Trim() + ";");
                    }
                }
            }
            else
            {
                list.Add(text);
            }
        }

        private string[] StringLinesParse(string[] lines)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < lines.Length; i++)
            {
                string input = lines[i].Trim();
                input = Regex.Replace(input, "\\s{2,}", " ");
                if (!string.IsNullOrEmpty(input))
                {
                    StringLineParse(list, input);
                }
            }
            return list.ToArray();
        }

        private string[] IndentParse(string[] result)
        {
            for (int i = 1; i < result.Length; i++)
            {
                string input = result[i - 1];
                if (Regex.IsMatch(input, "^if(.+)$", RegexOptions.Singleline) && !result[i].StartsWith("{"))
                {
                    result[i] = "___indent___" + result[i];
                }
                if (Regex.IsMatch(input, "^else$", RegexOptions.Singleline) && !result[i].StartsWith("{"))
                {
                    result[i] = "___indent___" + result[i];
                }
                if (Regex.IsMatch(input, "for(.+)", RegexOptions.Singleline) && !result[i].StartsWith("{"))
                {
                    result[i] = "___indent___" + result[i];
                }
                if (Regex.IsMatch(input, "foreach(.+)", RegexOptions.Singleline) && !result[i].StartsWith("{"))
                {
                    result[i] = "___indent___" + result[i];
                }
                if (Regex.IsMatch(input, "while(.+)", RegexOptions.Singleline) && !result[i].StartsWith("{"))
                {
                    result[i] = "___indent___" + result[i];
                }
            }
            return result;
        }

        private string CreateIndent(int length)
        {
            if (length <= 0)
            {
                return "";
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                stringBuilder.Append(" ");
            }
            return stringBuilder.ToString();
        }

        public string StringFormat(string content)
        {
            string text = string.Empty;
            string text2 = content;
            content = content.Replace('\r', ' ').Replace("///", "\n///").Replace("/*", "\n/*\n")
                .Replace("*/", "\n*/\n")
                .Replace("{", "\n{\n")
                .Replace("}", "\n}\n");
            string[] array = IndentParse(StringLinesParse(content.Split('\n')));
            int num = 4;
            int num2 = 0;
            string[] array2 = array;
            foreach (string text3 in array2)
            {
                if (text3.StartsWith("{"))
                {
                    text = text + CreateIndent(num2 * num) + text3 + "\n";
                    num2++;
                }
                else if (text3.StartsWith("}"))
                {
                    num2--;
                    text = text + CreateIndent(num2 * num) + text3 + "\n";
                }
                else if (text3.StartsWith("___indent___"))
                {
                    num2++;
                    text = text + CreateIndent(num2 * num) + text3.Replace("___indent___", "") + "\n";
                    num2--;
                }
                else
                {
                    text = text + CreateIndent(num2 * num) + text3 + "\n";
                }
            }
            return text;
        }
    }
}
